package com.newbingo.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;

import com.newbingo.dto.AgentDto;

import java.io.IOException;


public class LoginFilter implements Filter {
	private FilterConfig filterConfig;
	
	public void init(FilterConfig filterConfig){
		this.filterConfig=filterConfig;
	}
	
	public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain)
		throws ServletException,IOException{
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)resp;
		HttpSession session=request.getSession();
		Object object=session.getAttribute("user");
		if(object==null){
			response.sendRedirect("pages/login.jsp");
		}
		if(object!=null){
			if(object instanceof AgentDto){
				chain.doFilter(req,resp);
			}else{
				response.sendRedirect("pages/login.jsp");
			}
		}
	}
	
	public void destroy(){
		
	}
	
	
	public FilterConfig getFilterConfig() {
		return filterConfig;
	}

	public void setFilterConfig(FilterConfig filterConfig) {
		this.filterConfig = filterConfig;
	}
}
